package com.clms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clms.common.JwtUtils;
import com.clms.common.R;
import com.clms.common.util.Payload;
import com.clms.system.mapper.MenuMapper;
import com.clms.system.myconfig.RsaKeyProperties;
import com.clms.system.pojo.entitry.SysMenu;
import com.clms.system.pojo.entitry.SysUser;
import com.clms.system.pojo.util.WebUtil;
import com.clms.system.pojo.vo.OnlyoneVo;
import com.clms.system.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: clms-third
 * @description:动态左侧的生成
 * @author: 大鱼
 * @create: 2021-07-19 14:21
 **/
@Service
public class MenuServiceimpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    RsaKeyProperties properties;
    @Override
    public R findleftmenubyuserid() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Payload<SysUser> info = JwtUtils.getInfoFromToken(token, properties.getPublicKey(), SysUser.class);
        List<SysMenu> menus = menuMapper.findleftmenubyuserid((int) info.getUserInfo().getUserId());
        List<SysMenu> firstlist = new ArrayList<>();
        for (SysMenu menu:menus
             ) {
            if (menu.getParentId() == 0) {
                firstlist.add(menu);
            }
        }
        for (SysMenu menu : firstlist) {
            menu.setChildren(Findchildren(menus , (int) menu.getMenuId()));
        }
        return R.ok().put("menulist",firstlist).put("userinfo",info.getUserInfo());
    }

    @Override
    public R getMenuByRoleId(Integer roleId) {
        List<SysMenu> sysMenuList = menuMapper.findmenubyRoleid(roleId);
        List<Integer> rouMenuIds = sysMenuList.stream().map(item->{return  (int)item.getMenuId();}).collect(Collectors.toList());
        List<SysMenu> allmenu = menuMapper.selectList(null);
        List<SysMenu> firstmenus=allmenu.stream().filter(item->{return  item.getParentId()==0;}).collect(Collectors.toList());
        for (SysMenu m:firstmenus
             ) {
            m.setChildren(Findchildren(allmenu , (int) m.getMenuId()));
        }
        return R.ok().put("menus", firstmenus).put("roleMenuIds", rouMenuIds);
    }
   @Transactional
    @Override
    public R saveRoleMenu(Integer roleId, List<Integer> roleMenuIds) {
        menuMapper.deletebyroleId(roleId);
        menuMapper.insertrolemenu(roleMenuIds, roleId);
        return R.ok();
    }

    @Override
    public R getAllMenu() {
        List<SysMenu> allmenu = menuMapper.selectList(null);
        List<SysMenu> firstmenus=allmenu.stream().filter(item->{return  item.getParentId()==0;}).collect(Collectors.toList());
        for (SysMenu m:firstmenus
        ) {
            m.setChildren(Findchildren(allmenu , (int) m.getMenuId()));
        }
        return R.ok().put("menus", firstmenus);
    }

    @Override
    public R getMenuById(Integer menuId) {
        SysMenu menu= menuMapper.selectById( menuId);

        return R.ok().put("record",menu);
    }

    @Override
    public R addOrUpdateMenu(SysMenu menu) {
        if (menu.getMenuId() != 0) {
            menuMapper.updateById(menu);
            return R.ok().message("修改成功");
        }
        else {
            menuMapper.insert(menu);
            return R.ok().message("插入成功");
        }

    }

    @Override
    public R onlyOneCheckonlyOneCheck(OnlyoneVo onlyoneVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("menu_name", onlyoneVo.getSame());
        SysMenu menu = menuMapper.selectOne(queryWrapper);
        if (menu == null) {
          return R.ok().message("可以使用");
        }else {
            if (String.valueOf(menu.getMenuId()).equals(onlyoneVo.getId())) {
                return R.ok().message("未变化");
            }
            return R.error().message("重复");
        }
    }

    @Override
    public R onlyOneCheckonlyOneCheckUrl(OnlyoneVo onlyoneVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("url", onlyoneVo.getSame());
        SysMenu menu = menuMapper.selectOne(queryWrapper);
        if (menu == null) {
            return R.ok().message("可以使用");
        }else {
            if (String.valueOf(menu.getMenuId()).equals(onlyoneVo.getId())) {
                return R.ok().message("未变化");
            }
            return R.error().message("重复");
        }

    }

    public List<SysMenu> Findchildren(List<SysMenu> firstlist,Integer pid) {
       List<SysMenu> children = new ArrayList<>();
       for (SysMenu chil: firstlist) {
           if (chil.getParentId()==pid) {
               children.add(chil);
           }
       }
       for (SysMenu c : children) {
           c.setChildren(Findchildren(children, (int) c.getMenuId()));
       }
       return children;
   }

}
